package net.csdn.business.mirror.config;

import com.aliyun.openservices.ons.api.MessageListener;
import com.aliyun.openservices.ons.api.PropertyKeyConst;
import com.aliyun.openservices.ons.api.bean.ConsumerBean;
import com.aliyun.openservices.ons.api.bean.Subscription;
import com.google.common.collect.Maps;
import lombok.Data;
import net.csdn.business.mirror.listen.AliProjectMirrorMessageListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

import java.util.Map;
import java.util.Optional;
import java.util.Properties;

@Data
@Configuration
@RefreshScope
public class AliMqConfig {
    private static final String DEFAULT_MAX_TIMES = "5";
    private static final String DEFAULT_THREAD_NUMS = "5";

    @Value("${ali.mq.accessKey}")
    private String accessKey;

    @Value("${ali.mq.secretKey}")
    private String secretKey;

    @Value("${ali.mq.nameSrvAddr}")
    private String nameSrvAddr;

    @Value("${ali.mq.tcp.nameSrvAddr}")
    private String tcpNameSrvAddr;

    @Value("${ali.mq.groupId}")
    private String groupId;

    @Value("${ali.mq.instanceId}")
    private String instanceId;

    @Value("${ali.mq.topic}")
    private String topic;

    @Value("${ali.mq.consumer.max:5}")
    private String consumerMaxTimes;

    @Value("${ali.mq.consumer.thread.num:5}")
    private String consumerThreadNums;

    @Value("${ali.mq.tag:PROJECT_CHANGE}")
    private String tag;

    @Value("${ali.mq.consumerEnable:false}")
    private boolean consumerEnabled;

    @DependsOn("aliProjectMirrorMessageListener")
    @Bean(name = "aliProjectMirrorConsumer", initMethod = "start", destroyMethod = "shutdown")
    @ConditionalOnProperty(prefix = "ali.mq", name = "consumerEnable", havingValue = "true")
    public ConsumerBean initAliConsumer(AliProjectMirrorMessageListener aliProjectMirrorMessageListener) {
        return initConsumerBean(aliProjectMirrorMessageListener, topic, groupId, tag);
    }

    private Properties getMqProperties() {
        Properties properties = new Properties();
        properties.setProperty(PropertyKeyConst.AccessKey, this.accessKey);
        properties.setProperty(PropertyKeyConst.SecretKey, this.secretKey);
        properties.setProperty(PropertyKeyConst.NAMESRV_ADDR, this.tcpNameSrvAddr);
        return properties;
    }

    private ConsumerBean initConsumerBean(MessageListener consumerListener, String topic, String groupId, String tag) {
        return initConsumerBean(getMqProperties(), consumerListener, topic, groupId, tag, consumerMaxTimes, consumerThreadNums);
    }

    private ConsumerBean initConsumerBean(Properties properties, MessageListener consumerListener, String topic, String groupId, String tag, String consumerMaxTimes, String consumerThreadNums) {
        ConsumerBean consumerBean = new ConsumerBean();
        // 配置文件
        properties.setProperty(PropertyKeyConst.GROUP_ID, groupId);
        properties.setProperty(PropertyKeyConst.MaxReconsumeTimes, Optional.ofNullable(consumerMaxTimes).orElse(DEFAULT_MAX_TIMES));

        // 默认 20-64
        properties.setProperty(PropertyKeyConst.ConsumeThreadNums, Optional.ofNullable(consumerThreadNums).orElse(DEFAULT_THREAD_NUMS));

        consumerBean.setProperties(properties);
        // 订阅关系
        Map<Subscription, MessageListener> subscriptionTable = Maps.newHashMap();
        Subscription subscription = new Subscription();
        subscription.setTopic(topic);
        subscription.setExpression(tag);
        subscriptionTable.put(subscription, consumerListener);

        consumerBean.setSubscriptionTable(subscriptionTable);

        return consumerBean;
    }
}